package people.paper.tv.utils.monitor;

import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

import java.lang.reflect.Method;

//public class Monitor {
//    public static void start(Context context, String name) {
//        Intent start = new Intent("coocaa.intent.action.MONITOR_START");
//        start.putExtra("name", name);//传入采集指标名需要与上述json一致
//        start.putExtra("time", SystemClock.uptimeMillis());
//        LocalBroadcastManager.getInstance(context).sendBroadcast(start);
//    }
//
//    public static void end(Context context, String name) {
//        Intent start = new Intent("coocaa.intent.action.MONITOR_END");
//        start.putExtra("name", name);//传入采集指标名需要与上述json一致
//        start.putExtra("time",SystemClock.uptimeMillis());
//        LocalBroadcastManager.getInstance(context).sendBroadcast(start);
//    }
//}

public class Monitor {
    private static Method START, END;

    static {
        try {
            Class<?> clazz = Monitor.class.getClassLoader().loadClass("com.coocaa.monitor.model.TargetLog");
            START = clazz.getMethod("start", Intent.class);
            END = clazz.getMethod("end", Intent.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static final void start(Context context, String name) {
        try {
            Intent start = new Intent("coocaa.intent.action.MONITOR_START");
            start.putExtra("key", name);//传入采集指标名需要与上述json一致
            start.putExtra("time", SystemClock.uptimeMillis());
            if (START != null) {
                START.invoke(null, start);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static final void end(Context context, String name) {
        try {
            Intent end = new Intent("coocaa.intent.action.MONITOR_END");
            end.putExtra("key", name);//传入采集指标名需要与上述json一致
            end.putExtra("time", SystemClock.uptimeMillis());
            if (END != null) {
                END.invoke(null, end);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}